home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / smixw130.zip / MIXTEST.C < prev    next >
C/C++ Source or Header  |  1997-06-06  |  6KB  |  253 lines

  1. /*      SMIXW is Copyright 1995 by Ethan Brodsky.  All rights reserved      */
  2.  
  3. /* mixtest.c */
  4.  
  5. #include <conio.h>
  6. #include <graph.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10.  
  11. #include "detect.h"
  12. #include "smix.h"
  13.  
  14. #define ON  1
  15. #define OFF 0
  16.  
  17. #define TRUE  1
  18. #define FALSE 0
  19.  
  20. #define NUMSOUNDS 6
  21.  
  22. #define random(num) (int)(((long)rand()*(num))/(RAND_MAX+1))
  23. #define randomize() srand((unsigned)time(NULL))
  24.  
  25. char *resource_file = "mixtest.snd";
  26.  
  27. char *sound_key[NUMSOUNDS] =
  28.   {
  29.     "JET",
  30.     "GUN",
  31.     "CRASH",
  32.     "CANNON",
  33.     "LASER",
  34.     "GLASS"
  35.   };
  36.  
  37. int baseio, irq, dma, dma16;
  38.  
  39. SOUND *sound[NUMSOUNDS];
  40.  
  41. void ourexitproc(void)
  42.   {
  43.     int i;
  44.  
  45.     for (i=0; i < NUMSOUNDS; ++i)
  46.       if (sound[i] != NULL)
  47.         free_sound(sound+i);
  48.   }
  49.  
  50. void loadsounds(void)
  51.   {
  52.     int i;
  53.  
  54.     printf("Loading sounds\n");
  55.  
  56.     if (!open_sound_resource_file(resource_file))
  57.       {
  58.         printf("ERROR:  Can't load sound resource file\n");
  59.         exit(EXIT_FAILURE);
  60.       }
  61.  
  62.     for (i=0; i < NUMSOUNDS; i++)
  63.       load_sound(&(sound[i]), sound_key[i]);
  64.  
  65.     close_sound_resource_file();
  66.  
  67.     atexit(ourexitproc);
  68.   }
  69.  
  70. void freesounds(void)
  71.   {
  72.     int i;
  73.  
  74.     for (i = 0; i < NUMSOUNDS; i++)
  75.       free_sound(sound+i);
  76.   }
  77.  
  78. void init(void)
  79.   {
  80.     printf("-------------------------------------------\n");
  81.     printf("Sound Mixing Library v1.30 by Ethan Brodsky\n");
  82.     if (!detect_settings(&baseio, &irq, &dma, &dma16))
  83.       {
  84.         printf("ERROR:  Invalid or non-existant BLASTER environment variable!\n");
  85.         exit(EXIT_FAILURE);
  86.       }
  87.     else
  88.       {
  89.         if (!init_sb(baseio, irq, dma, dma16))
  90.           {
  91.             printf("ERROR:  Error initializing sound card!\n");
  92.             exit(EXIT_FAILURE);
  93.           }
  94.       }
  95.  
  96.     printf("BaseIO=%Xh     IRQ%u     DMA8=%u     DMA16=%u\n", baseio, irq, dma, dma16);
  97.  
  98.     printf("DSP version %.1u.%.02u:  ", dspversion>>8, dspversion&0xFF);
  99.     if (sixteenbit)
  100.       printf("16-bit, ");
  101.     else
  102.       printf("8-bit, ");
  103.     if (autoinit)
  104.       printf("Auto-initialized\n");
  105.     else
  106.       printf("Single-cycle\n");
  107.  
  108.     init_mixing();
  109.  
  110.     printf("\n");
  111.   }
  112.  
  113. void shutdown(void)
  114.   {
  115.     shutdown_mixing();
  116.     shutdown_sb();
  117.   }
  118.  
  119. int main(void)
  120.   {
  121.     int  jet        = 0;
  122.     int  randsound  = 1;
  123.     int  rate       = 22000;
  124.     int  volume     = 255;
  125.  
  126.     struct rccoord coords;
  127.     int  stop;
  128.     long counter;
  129.     char inkey;
  130.     int  num;
  131.  
  132.     randomize();
  133.  
  134.     init();
  135.  
  136.     loadsounds();
  137.  
  138.     printf("Press:                       \n");
  139.     printf("  J   Toggle jet engine      \n");
  140.     printf("  R   Toggle random sounds   \n");
  141.     printf("  1   Machine Gun            \n");
  142.     printf("  2   Crash                  \n");
  143.     printf("  3   Cannon                 \n");
  144.     printf("  4   Laser                  \n");
  145.     printf("  5   Breaking glass         \n");
  146.     printf("  -   Decrease volume        \n");
  147.     printf("  +   Increase volume        \n");
  148.     printf("  <   Reduce sampling rate   \n");
  149.     printf("  >   Increase sampling rate \n");
  150.     printf("  Q   Quit                   \n");
  151.  
  152.     set_sound_volume(volume);
  153.  
  154.     coords = _gettextposition();
  155.  
  156.     stop = FALSE;
  157.     counter = 0;
  158.  
  159.     do
  160.       {
  161.        /* Increment and display counters */
  162.         counter++;
  163.         printf("%8lu %8lu %4u %8u %8u\n", counter, intcount, voicecount, volume, rate);
  164.         _settextposition(coords.row-1, 1);
  165.  
  166.        /* Maybe start a random sound */
  167.         if (randsound && (random(2500) == 0))
  168.           {
  169.            num = (random(NUMSOUNDS-1))+1;
  170.            start_sound(sound[num], num, 64+random(192), OFF);
  171.           }
  172.  
  173.        /* Start a sound if a key is pressed */
  174.         if (kbhit())
  175.           {
  176.             inkey = getch();
  177.             if ((inkey >= '0') && (inkey <= '9'))
  178.               {
  179.                 num = inkey - '0'; /* Convert to integer */
  180.                 if (num < NUMSOUNDS)
  181.                   start_sound(sound[num], num, 255, OFF);
  182.               }
  183.             else
  184.               {
  185.                 switch(inkey)
  186.                   {
  187.                     case 'j':
  188.                     case 'J':
  189.                       jet = !jet;
  190.                       if (jet)
  191.                         start_sound(sound[0], 0, 255, ON);
  192.                       else
  193.                         stop_sound(0);
  194.                       break;
  195.  
  196.                     case 'R':
  197.                     case 'r':
  198.                       randsound = !randsound;
  199.                       break;
  200.  
  201.                     case '-':
  202.                     case '_':
  203.                       volume -= 4;
  204.                       if (volume < 0)
  205.                         volume = 0;
  206.                       set_sound_volume(volume);
  207.                       break;
  208.  
  209.                     case '+':
  210.                     case '=':
  211.                       volume += 4;
  212.                       if (volume > 255)
  213.                         volume = 255;
  214.                       set_sound_volume(volume);
  215.                       break;
  216.  
  217.                     case '<':
  218.                     case ',':
  219.                       rate -= 250;
  220.                       if (rate < 5000)
  221.                         rate = 5000;
  222.                       set_sampling_rate(rate);
  223.                       break;
  224.  
  225.                     case '>':
  226.                     case '.':
  227.                       rate += 250;
  228.                       if (rate > 48000)
  229.                         rate = 48000;
  230.                       set_sampling_rate(rate);
  231.                       break;
  232.  
  233.                     default:
  234.                       stop = TRUE;
  235.                       break;
  236.                   }
  237.               }
  238.           }
  239.       }
  240.     while (!stop);
  241.  
  242.     if (jet)
  243.       stop_sound(0);
  244.  
  245.     shutdown();
  246.  
  247.     freesounds();
  248.  
  249.     printf("\n");
  250.  
  251.     return(0);
  252.   }
  253.